package propertiesReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesReader {

	private boolean endproductrdy;

	
	//Dev var
	private String localStorage ="D:/Arbeit/Java-Workspace/emailProject/src/main/resources/";


	public PropertiesReader(boolean endproduct) {
		endproductrdy = endproduct;
	}

	/**
	 * 
	 * @param key
	 * @return the set property in the file folder.properties
	 */
	public String getProperty(String key) {
		Properties properties = new Properties();
		String property = "";
		try {
			// Get the properties file over getResourceAsStream
			if (endproductrdy) {
				// FileInputStream for the endproduct
				properties.load(new FileInputStream(new File("./email.properties")));
				
			} else {
				// Get the properties file over getResourceAsStream
				properties.load(
						new FileInputStream(localStorage + "email.properties"));
			}
			property = properties.getProperty(key);
			if (property == null || property.equals("null")) {
				System.err.println(key + " not found OR not set yet!");
				throw new IOException("Fehler");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return property;
	}

	public void store(String tmpAttachmentPath, String tmpXlsPath) {
		Properties properties = new Properties();
		try {
			if (endproductrdy) {
				// FileInputStream for the endproduct
				properties.load(new FileInputStream("./email.properties"));
			} else {
				// Get the properties file over getResourceAsStream
				properties.load(new FileInputStream(localStorage + "email.properties"));
				//properties.load(getClass().getClassLoader().getResourceAsStream("email.properties"));
			}
			// Set Properties
			properties.setProperty("attachmentPath", tmpAttachmentPath);
			properties.setProperty("xlsPath", tmpXlsPath);
			FileOutputStream out = null;
			if (endproductrdy) {
				out = new FileOutputStream("email.properties");

			} else {
				//Testing
				out = new FileOutputStream(
						localStorage + "email.properties");
			}
			properties.store(out, "-");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//	public void setProperty(String key, String value) {
//		Properties properties = new Properties();
//		String property = "";
//		try {
//			if (endproductrdy) {
//				// FileInputStream for the endproduct
//				properties.load(new FileInputStream("resources/email.properties"));
//			} else {
//				// Get the properties file over getResourceAsStream
//				InputStream stream = this.getClass().getResourceAsStream(
//						localStorage + "email.properties");
//				properties.load(stream);
//				stream.close();
//			}
//			properties.setProperty(key, value);
//			if (property == null || property.equals("null")) {
//				System.err.println(key + " not found OR not set yet!");
//				throw new IOException("Fehler");
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

}
